<?php

/**
 * Created by PhpStorm.
 * User: locove
 * Date: 15-4-23
 * Time: 下午3:01
 * 采购提成计算每个sku最长交期
 */
class Controller_Longlead extends Controller_Admin
{
    public function action_index()
    {
        $this->title = __("上个月SKU最长交期下载");
        $view        = View::factory('longlead');
        $this->response->body($view);
    }

    public function action_download()
    {
        //动态生成表
        Longlead::creatTbl();

        //插入数据
        $minDate = date('Y-m-01',strtotime('-1 month'));
        $maxDate = date('Y-m-t',strtotime('-1 month'));
        Longlead::longleads($minDate,$maxDate);

        //下载
        $sql   = "SELECT `sku`, TRUNCATE( `ldt_sec` /86400, 3 ) `longest` FROM `ldt_ords` ";
        $lists = DB::query(Database::SELECT,$sql)
            ->execute('newerp')
            ->as_array();
        if(!empty($lists)){
            set_time_limit(0);
            ini_set('memory_limit','1024M');
            require_once(Kohana::find_file('vendor','PHPExcel/PHPExcel/IOFactory'));
            $this->objReader = PHPExcel_IOFactory::createReader('Excel5');
            $objPHPExcel     = new PHPExcel();
            $objPHPExcel->getProperties()
                ->setCreator("yks")
                ->setLastModifiedBy("yks")
                ->setTitle("yks")
                ->setSubject("Offic 2007 XLSX Test Document")
                ->setDescription("Test document for PHPExcel, generated using PHP classes.")
                ->setKeywords("offic 2007 openxml php")
                ->setCategory("yks");
            $objActSheet = $objPHPExcel->setActiveSheetIndex(0);
            $objActSheet->setCellValue('A1','SKU');
            $objActSheet->setCellValue('B1','交期');
            $objActSheet->getStyle('A1:B1')
                ->getAlignment()
                ->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()
                ->getStyle('A1')
                ->getFont()
                ->setSize(12)
                ->setBold(TRUE);
            $objPHPExcel->getActiveSheet()
                ->getStyle('B1')
                ->getFont()
                ->setSize(12)
                ->setBold(TRUE);
            $objActSheet->getStyle('A1:B1')
                ->getBorders()
                ->getAllBorders()
                ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            $objPHPExcel->getActiveSheet()
                ->getColumnDimension('A')
                ->setWidth(14);
            $objPHPExcel->getActiveSheet()
                ->getColumnDimension('B')
                ->setWidth(14);
            $key = 2;
            foreach($lists as $v){
                $objActSheet->setCellValue('A'.$key,$v['sku']);
                $objActSheet->setCellValue('B'.$key,$v['longest']);
                $objActSheet->getStyle('A'.$key.':B'.$key)
                    ->getAlignment()
                    ->setHorizontal(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $objActSheet->getStyle('A'.$key.':B'.$key)
                    ->getBorders()
                    ->getAllBorders()
                    ->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
                $key++;
            }

            $date     = date('Ymd');
            $filename = "上个月sku最长交期表".$date.".xls";
            header("Content-Type: application/vnd.ms-excel; charset=utf-8");
            header('Content-Disposition: attachment;filename='.$filename);
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
            $objWriter->save('php://output');
            //exit;
        }

        //动态删除表
        Longlead::dropTbl();
    }
}
